package com.selune.online.ordering.system.project.tenant.service;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.selune.online.ordering.system.project.api.base.entity.GoodsCategory;
import com.selune.online.ordering.system.project.api.base.response.Response;
import com.selune.online.ordering.system.project.api.mapper.GoodsCategoryMapper;
import com.selune.online.ordering.system.project.api.pojo.category.dto.GoodsCategoryDTO;
import com.selune.online.ordering.system.project.api.pojo.category.vo.GoodsCategoryVO;
import com.selune.online.ordering.system.project.api.service.tenant.ITenantCategoryService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商户端商品类目Service
 *
 * @author xiaoyp
 * @date 2020/2/29
 */

@Service
@Slf4j
public class TenantCategoryServiceImpl implements ITenantCategoryService {
	
	@Resource
	private GoodsCategoryMapper goodsCategoryMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Response<Boolean> saveCategory(GoodsCategoryDTO goodsCategoryDTO) {
		
		
		GoodsCategory goodsCategory = new GoodsCategory();
		
		BeanUtils.copyProperties(goodsCategoryDTO, goodsCategory);
		int result = goodsCategoryMapper.insert(goodsCategory);
		if (result == 1) {
			return Response.success("添加商品类目成功!", Boolean.TRUE);
		} else {
			return Response.error("添加商品类目失败");
		}
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Response<Boolean> updateCategory(Long categoryId, GoodsCategoryDTO goodsCategoryDTO) {
		if (null == categoryId) {
			log.error("ACCESS_LOG[]TenantCategoryServiceImpl[]updateCategory[]category_id is null!");
			return Response.error("category.id.is.null");
		}
		GoodsCategory goodsCategory = goodsCategoryMapper.selectById(categoryId);
		if (ObjectUtils.isEmpty(goodsCategory)) {
			log.error("ACCESS_LOG[]TenantCategoryServiceImpl[]updateCategory[]category_id is not exist!");
			return Response.error("category.id.is.not.exist");
		}
		
		if (StringUtils.isBlank(goodsCategoryDTO.getCategoryName())) {
			log.error("ACCESS_LOG[]TenantCategoryServiceImpl[]updateCategory[]category parameter is null!");
			return Response.error("category.param.is.null");
		} else {
			goodsCategoryMapper.updateById(new GoodsCategory()
					.setCategoryId(categoryId)
					.setCategoryName(goodsCategoryDTO.getCategoryName())
					.setCategoryDescription(goodsCategoryDTO.getCategoryDescription()));
			
			log.info("ACCESS_LOG[]TenantCategoryServiceImpl[]updateCategory[]update category success!");
			return Response.success("更新商品类目成功!", Boolean.TRUE);
		}
	}
	
	@Override
	public Response<List<GoodsCategoryVO>> listCategory() {
		List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectList(
			Wrappers.<GoodsCategory>lambdaQuery().eq(GoodsCategory::getIsDelete, Boolean.FALSE)
		);
		List<GoodsCategoryVO> goodsCategoryVOList = new ArrayList<>(goodsCategoryList.size());
		goodsCategoryList.forEach(goodsCategory -> {
			GoodsCategoryVO goodsCategoryVO = new GoodsCategoryVO();
			BeanUtils.copyProperties(goodsCategory, goodsCategoryVO);
			goodsCategoryVOList.add(goodsCategoryVO);
		});
		
		return Response.success(goodsCategoryVOList);
	}
	
	@Override
	public Response<GoodsCategoryVO> findCategoryById(Long categoryId) {
		GoodsCategory goodsCategory = goodsCategoryMapper.selectById(categoryId);
		GoodsCategoryVO goodsCategoryVO = new GoodsCategoryVO();
		BeanUtils.copyProperties(goodsCategory, goodsCategoryVO);
		return Response.success(goodsCategoryVO);
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Response<Boolean> deleteCategory(Long categoryId) {
		int result = goodsCategoryMapper.update(
			null,
			Wrappers.<GoodsCategory>lambdaUpdate()
				.eq(GoodsCategory::getCategoryId, categoryId)
				.set(GoodsCategory::getIsDelete, Boolean.TRUE)
		);
		if (result == 1) {
			return Response.success("删除商品类目成功!", Boolean.TRUE);
		} else {
			return Response.error("删除商品类目失败");
		}
	}
}
